package cn.hd.controller;

import cn.hd.entity.Address;
import cn.hd.entity.Comment;
import cn.hd.entity.Result;
import cn.hd.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//所有方法，返回的都是json格式的数据
@RestController //返回的都是json格式的数据
@CrossOrigin    //实现数据跨域
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/selectCommentAll")
    public Object selectCommentAll(int pageNum, int pageSize){
        PageInfo<Comment> page = commentService.selectAll(pageNum, pageSize);
        if (page.getList().size()==0){
            return new Result("400","查无此数据",null);
        }
        return new Result("200","查询成功",page);
    }

    @RequestMapping("/selectCommentAlls")
    public Object selectCommentAlls(int pageNum, int pageSize,long pid){
        Map map = new HashMap();
        map.put("pid",pid);
        PageInfo<Comment> page = commentService.selectAllByPid(pageNum,pageSize,map);
        if (page.getList().size()==0){
            return new Result("400","查无此数据",null);
        }
        return new Result("200","查询成功",page);
    }

    @RequestMapping("/selectCommentAllsrp")
    public Object selectCommentAllsrp(long pid){
        List<Comment> comments = commentService.selectCommentAllsrp(pid);
        if (comments.size()==0){
            return new Result("400","查无此数据",null);
        }
        return new Result("200","查询成功",comments);
    }


    @RequestMapping("/addComment")
    public  Object addComment(String textarea,long value,Long uid,String pid){
        int pi = Integer.parseInt(pid);
        Comment comment = new Comment();
        comment.setMessage(textarea);
        comment.setPleased(value);
        int i = commentService.addComment(comment,uid);
        if (i!=0){
            int k = commentService.addUserComment(uid, comment.getCid(),pi);
            if (k!=0){
                    return new Result("200","添加成功！",i);
            }else {
                return new Result("400","添加失败!",null);
            }
        }else {
            return new Result("400","添加失败!",null);
        }
    }

    //删除
    @RequestMapping("/deleteComment")
    public Object deleteComment(long cid){
        int i = commentService.deleteComment(cid);
        if (i!=0){
            int k = commentService.deleteCommentUser(cid);
            if (k!=0){
                return new Result("200","删除成功！",null);
            }else{
                return new Result("400","删除失败!",null);
            }
        }else {
            return new Result("400","删除失败!",null);
        }
    }

    //修改
    @RequestMapping("/updateComment")
    public Object updateComment(Comment comment){
        int i = commentService.updateComment(comment);
        if (i!=0){
            return new Result("200","修改成功！",null);
        }else {
            return new Result("400","修改失败!",null);
        }
    };
}
